<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\goods\Category;
use app\common\model\goods\Review;
use app\common\model\shop\SkuGoods;

/**
 * 商品管理接口
 */
class Goods extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['goods_category', 'goods_list', 'goods_info','review_list'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
     * @Author LK
     * @ApiTitle (商品分类)
     * @ApiMethod (get)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function goods_category()
    {
        $list = Category::all();
        $this->success('返回成功', $list);
    }

    /**
     * @Author LK
     * @ApiTitle (商品列表)
     * @ApiMethod (get)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="分类ID")
     * @ApiParams (name="type", type="string", required=true, sample="", description="1=普通商品,2=会员商品,3=一折专区")
     * @ApiParams (name="keywords", type="string", required=true, sample="", description="关键字")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function goods_list()
    {
        $param = request()->param();
        extract($param);
        $goods = new SkuGoods();
        if (isset($id) && $id > 0) {
            $goods->where('category_id', $id);
        }
        if (isset($keywords) && $keywords) {
            $goods->whereLike('name', '%' . $keywords . '%');
        }
        if ($type == 1) {
            $goods->whereIn('type', ['1', '2']);
        }
        if ($type == 2) {
            $goods->where('type', '4');
        }
        if ($type == 3) {
            $goods->where('type', '3');
        }
        $lists =  $goods->where('status', '1')->order('weigh desc,id desc')->paginate();
        $this->success('返回成功', $lists);
    }

    /**
     * @Author LK
     * @ApiTitle (商品详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function goods_info()
    {
        $param = request()->param();
        extract($param);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }

        $goods = SkuGoods::get($id);
        $goods->sku = SkuGoods::getVueInitData($id);
        if (!$goods) {
            $this->error('商品不存在');
        }
        $this->success('返回成功', $goods);
    }
    /**
     * @Author LK
     * @ApiTitle (商品评论)
     * @ApiMethod (GET)
     * @ApiParams (name="goods_id", type="integer", required=true, sample="", description="商品id")
     * @ApiParams (name="size", type="integer", required=true, sample="", description="每页数量")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function review_list()
    {
        $param = request()->param();
        extract($param);
        if (!isset($goods_id) || !$goods_id) {
            $this->error('参数错误');
        }
        $list = Review::with(['user'])->where('goods_id', $goods_id)->paginate($size ?? 10);
        $this->success('返回成功', $list);
    }
}
